package com.study.controller;

import com.study.bean.Blog;
import com.study.bean.BlogQuery;
import com.study.service.IBlogService;
import com.study.service.impl.BlogServiceImpl;
import com.study.util.JSONUtil;
import com.study.util.MyBeanUtils;
import com.study.util.PageInfo;
import com.study.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private IBlogService blogService = new BlogServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                try {
                    selectByPage(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "deleteById":
                try {
                    deleteById(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "deleteAll":
                try {
                    deleteAll(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "add":
                try {
                    add(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "selectById":
                try {
                    selectById(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "update":
                try {
                    update(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "updateStatus":
                try {
                    updateStatus(req, resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
        }
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {

    }
    private void update(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        System.out.println("BlogServlet.update");
        //String id = req.getParameter("id");
        //String name = req.getParameter("name");
        //String content = req.getParameter("content");
        //Blog blog = new Blog();
        //blog.setId(Integer.parseInt(id));
        //blog.setName(name);
        //blog.setContent(content);

        Blog blog = MyBeanUtils.copyToBean(req, Blog.class);
        blogService.update(blog);
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        System.out.println("BlogServlet.selectById");
        String id = req.getParameter("id");
        Blog blog = blogService.selectById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok(blog));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        System.out.println("BlogServlet.add");
        //String name = req.getParameter("name");
        //
        //String content = req.getParameter("content");
        //
        //
        //Blog blog = new Blog();
        //blog.setName(name);
        //blog.setContent(content);
        Blog blog = MyBeanUtils.copyToBean(req, Blog.class);
        blogService.add(blog);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        System.out.println("BlogServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        blogService.deleteAll(ids);
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        System.out.println("BlogServlet.deleteById");
        String id = req.getParameter("id");
        blogService.deleteById(Integer.parseInt(id));
        /*Result result = new Result();
        result.setCode(0);
        result.setMsg("删除成功");
        JSONUtil.toJSON(resp, result);*/
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    // /blog?method=selectByPage&page=1&limit=10
    // /blog?method=selectByPage&page=1&limit=10&name=zhansgsan&address=5&time=2014
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        System.out.println("BlogServlet.selectByPage");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String content = req.getParameter("content");

        System.out.println(page + limit + name);
        BlogQuery blogQuery = new BlogQuery(Integer.parseInt(page), Integer.parseInt(limit), name,content);
        PageInfo pageInfo = blogService.selectByPage(blogQuery);

        JSONUtil.toJSON(resp, pageInfo);
    }
}
